﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using text.Utils;

namespace text
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitCity();
            }

          
        }
        public void InitCity()
        {
            string sql = ("select * from S_City");
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            ddl.DataSource = ds;
            ddl.DataTextField = "CityName";
            ddl.DataValueField = "CityID";
            ddl.DataBind();
        }


        protected void Button1_Click1(object sender, EventArgs e)
        {
            string img = FileUpload1.FileName;
          
            string name = ".jpg.png.gif.jfif";
            string exname = img.Substring(img.LastIndexOf('.'));
            if (FileUpload1.FileContent.Length > 1048576)
            {
                Response.Write("<script>alert('文件过大')</script>");
                return;
            }
            if (name.Contains(exname) == false)
            {
                Response.Write("<script>alert('文件不符合')</script>");
                return;
            }
            string path = Server.MapPath("images") + "\\" + img;
            FileUpload1.SaveAs(path);
            Image1.ImageUrl = "/images/" + img;
        }

        protected void btnregister_Click(object sender, EventArgs e)
        {
            string name = txtname.Text.Trim();
            string pwd = txtpwd.Text.Trim();
            string phone = txtphone.Text.Trim();
            string img = Image1.ImageUrl.Trim();
            int dl = Convert.ToInt32(ddl.SelectedValue);
            int sex;
            if (rb1.Checked)
            {
                sex = 0;
            }
            else
            {
                sex = 1;
            }
            string email = txtemail.Text.Trim();
            string qq = txtqq.Text.Trim();
            int x = 0;
            string birthday =txtbirthday.Text.Trim();
          string sql = string.Format("insert into users values('{0}','{1}',{2},{3},'{4}','{5}','{6}','{7}',{8},{9},'{10}',{11},'{12}')", name, pwd, dl, sex, img, phone, email, qq, x, x, DateTime.Now, x,birthday);
            string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection conn1 = new SqlConnection(conn);
            SqlCommand cmd = new SqlCommand(sql, conn1);
            conn1.Open();
            int flag = cmd.ExecuteNonQuery();
            conn1.Close();
            if (flag > 0)
            {
                // Response.Write("<script>alert('注册成功');window.location.href='login.aspx'</script>");
                Response.Write("<script>alert('注册成功')</script>");
            }
        }
    }
}